package com.web;

import com.pojo.Page;
import com.pojo.Tour;
import com.service.TourService;
import com.service.impl.TourServiceImpl;
import com.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.print.Book;
import java.io.IOException;
import java.util.List;

/**
 * @Author hjb
 * @Version 1.0
 * @Contact 1754729389@qq.com
 */
public class TourServlet extends BaseServlet{

    private TourService tourService = new TourServiceImpl();


    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        int pageNo = Integer.parseInt(req.getParameter("pageNo"));
        pageNo++;


        //获取请求参数封装为bean对象

        Tour tour = WebUtils.copyParamToBean(req.getParameterMap(),new Tour());

        //调用tourService.addTour()方法添加旅行团信息

        System.out.println(tour);

        tourService.addTour(tour);

        //通过/manage/tourService?action=list跳转回tour_main.jsp页面

        resp.sendRedirect(req.getContextPath() + "/manage/tourServlet?action=page&pageNo=" + pageNo);

    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求的参数id
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        //调用tourService.deleteTourById();删除旅行团
        tourService.deleteTourById(id);
        //重定向回旅行团列表管理页面/tour/manage/tourServlet?action=list
        resp.sendRedirect(req.getContextPath() + "/manage/tourServlet?action=page&pageNo=" + req.getParameter("pageNo"));
    }

    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1、获取请求的参数==封装成为Tour对象
        Tour tour = WebUtils.copyParamToBean(req.getParameterMap(),new Tour());
        //2、调用tourService.updateTour(tour)修改旅行团
        tourService.updateTour(tour);
        //3、重定向回旅行团列表管理页面
        //地址：/工程名/manage/tourServlet?action=list
        resp.sendRedirect(req.getContextPath() + "/manage/tourServlet?action=page&pageNo=" + req.getParameter("pageNo"));
    }

    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //查询所有的旅行团信息
        List<Tour> tours = tourService.queryTours();

        //保存到request域中
        req.setAttribute("tours",tours);

        //请求转发到tour_main.jsp页面
        req.getRequestDispatcher("/pages/tour/tour_main.jsp").forward(req,resp);
    }

    protected void getTour(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1 获取请求的参数旅行团编号
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        //2 调用tourService.queryTourById查询旅行团
        Tour tour = tourService.queryTourById(id);
        //3 保存到旅行团到Request域中
        req.setAttribute("tour", tour) ;
        //4 请求转发到。pages/manage/tour_add.jsp页面
        req.getRequestDispatcher("/pages/tour/tour_add.jsp").forward(req,resp);
    }

    /**
     * 处理分页功能
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1 获取请求的参数 pageNo 和 pageSize
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        //2 调用TourService.page(pageNo，pageSize)：Page对象
        Page<Tour> page = tourService.page(pageNo,pageSize);

        page.setUrl("manage/tourServlet?action=page");
        System.out.println(page);
        //3 保存Page对象到Request域中
        req.setAttribute("page",page);
        //4 请求转发到pages/tour/tour_main.jsp页面
        req.getRequestDispatcher("/pages/tour/tour_main.jsp").forward(req,resp);
    }

}
